{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "1-效率对比",
   "id": "f561b84d227b38e8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "b = np.array(a)\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2-创建一维数组",
   "id": "3720e59c59c0ec85"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "list1 = [1,2,3,4]\n",
    "oneArray = np.array(list1)\n",
    "print(type(oneArray))\n",
    "print(oneArray)\n",
    "# 创建数组的多种形式\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1,2,3])\n",
    "print(t1)\n",
    "print(type(t1))\n",
    "'''\n",
    "[1 2 3]\n",
    "<class 'numpy.ndarray'>\n",
    "'''\n",
    "# 2. 传入range生成序列\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))\n",
    "'''\n",
    "[0 1 2 3 4 5 6 7 8 9]\n",
    "<class 'numpy.ndarray'>\n",
    "'''\n",
    "# 3. 使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0,10,2)\n",
    "print(t3)\n",
    "print(type(t3))\n",
    "'''\n",
    "[0 2 4 6 8]\n",
    "<class 'numpy.ndarray'>\n",
    "'''"
   ],
   "id": "84a3d4a457b0c05a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "3-创建二维数组",
   "id": "8ea8491ab4897b43"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "list2 = [[1,2],[3,4],[5,6]]\n",
    "twoArray = np.array(list2)\n",
    "print(twoArray)"
   ],
   "id": "541c7d53aefe10c2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "4-常用属性",
   "id": "b5c88f7c3d605e5c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "list2 = [[1,2],[3,4],[5,6]]\n",
    "twoArray = np.array(list2)\n",
    "# 获取数组的维度( 注意： 与函数的参数很像)\n",
    "print(twoArray.ndim)\n",
    "# 形状（行，列）\n",
    "print(twoArray.shape)\n",
    "# 有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "ab5e7575c2abeb06",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "5-调整数组的形状",
   "id": "e8a7fdd6b21f2faf"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "four = np.array([[1,2,3],[4,5,6]])\n",
    "# 修改的是原有的\n",
    "four.shape = (3,2)\n",
    "print(four)\n",
    "# 返回一个新的数组\n",
    "four = four.reshape(3,2)\n",
    "print(four)\n",
    "# 将多维变成一维数组\n",
    "five = four.reshape((6,),order='F')\n",
    "# 默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "six = four.flatten(order='F')\n",
    "print(five)\n",
    "print(six)\n",
    "# 拓展：数组的形状\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "t1 = t.reshape((4,6))\n",
    "print(t1)\n",
    "print(t1.shape)"
   ],
   "id": "e884fc0b4404c5af",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "6-数组转成list",
   "id": "b00895a4f979ab34"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 将数组转成list\n",
    "a= np.array([9, 12, 88, 14, 25])\n",
    "list_a = a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ],
   "id": "3245312b6242fdbf",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "7-数组和数的计算",
   "id": "85f2970198b7985e"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "t1 =np.arange(24).reshape((6,4))\n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2)\n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ],
   "id": "f24c3bb3ee275e50",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "8-数组与数组之间的操作",
   "id": "2b7b81bf166fa3c2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 同种形状的数组(对应位置进行计算操作)\n",
    "t1 = np.arange(24).reshape((6,4))\n",
    "t2 = np.arange(100,124).reshape((6,4))\n",
    "print(t1+t2)\n",
    "print(t1, t2)\n",
    "\n",
    "# 不种形状的多维数组不能计算\n",
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(18).reshape((3,6))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1-t2)\n",
    "'''\n",
    "ValueError: operands could not be broadcast together with shapes (4,6) (3,6)\n",
    "'''"
   ],
   "id": "da62759bf605b829",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "9-轴计算",
   "id": "655523b9365b7d2a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(np.sum(a,axis=0)) # [5 7 9]\n",
    "print(np.sum(a,axis = 1)) # [ 6 15]\n",
    "print(np.sum(a))\n",
    "a=np.arange(27).reshape((3,3,3))\n",
    "print(a)\n",
    "b=np.sum(a, axis=0)\n",
    "print(\"-\"*20)\n",
    "print(b)\n",
    "c=np.sum(a, axis=1)\n",
    "print(\"-\"*20)\n",
    "print(c)\n",
    "d=np.sum(a, axis=2)\n",
    "print(\"-\"*20)\n",
    "print(d)"
   ],
   "id": "ba1163a7f35e7d5a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "10-索引与切片",
   "id": "9766ea45ca42b856"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作 print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "# 如果只放置一个参数， 如 [2]， 将返回与该索引相对应的单个元素\n",
    "print(a[2],a)\n",
    "# 如果为 [2:]， 表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])\n",
    "\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*'*20)\n",
    "print(t1[1]) # 取一行(一行代表是一条数据， 索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*'*20)\n",
    "print(t1[1:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[1:3,:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3]])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3],:])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[:,1])# 取一列\n",
    "print('*'*20)\n",
    "print(t1[:,1:])# 连续的多列\n",
    "print('*'*20)\n",
    "print(t1[:,[0,2,3]])# 取不连续的多列\n",
    "print('*'*20)\n",
    "print(t1[2,3])# # 取某一个值,三行四列\n",
    "print('*'*20)"
   ],
   "id": "3bfe6c5926233ba1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "11-修改值",
   "id": "3964459307d22a7f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-04T07:11:57.877022Z",
     "start_time": "2025-08-04T07:11:57.857803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "# # 修改某一行的值\n",
    "t[1, :] = 0\n",
    "# #\n",
    "# 修改某一列的值\n",
    "t[:, 1] = 0\n",
    "import numpy as np\n",
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作 print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "# 如果只放置一个参数， 如 [2]， 将返回与该索引相对应的单个元素\n",
    "print(a[2],a)\n",
    "# 如果为 [2:]， 表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])\n",
    "# #\n",
    "# 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "# #\n",
    "# 修改连续多列\n",
    "t[:, 1:4] = 0\n",
    "# #\n",
    "# 修改多行多列， 取第二行到第四行， 第三列到第五列\n",
    "# t[1:4,2:5]=0\n",
    "# #\n",
    "# 修改多个不相邻的点\n",
    "# t[[0,1],[0,3]]=0\n",
    "# print(t)\n",
    "# 可以根据条件修改， 比如讲小于 10 的值改掉\n",
    "t[t < 10] = 0\n",
    "# print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and & # np.logical_or |\n",
    "# np.logical_not ~\n",
    "t[(t > 2) & (t < 6)] = 0 # 与\n",
    "t[(t < 2) | (t > 6)] = 0 # 或\n",
    "t[~(t > 6)] = 0 # 非\n",
    "# print(t)\n",
    "t=t.clip(10,18)\n",
    "print(t)\n",
    "# # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)， 输出 x， 不满足输出y。 )）\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result )"
   ],
   "id": "8ab65e40ec6d7955",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]]\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "12-数组的添加",
   "id": "be33e6cca09f990e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-04T07:21:00.312246Z",
     "start_time": "2025-08-04T07:21:00.281223Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. numpy.append 函数在数组的末尾添加值。 追加操作会分配整个数组， 并把原来的数组复制到新数组中。\n",
    "# 此外， 输入数组的维度必须匹配否则将生成 ValueError。\n",
    "'''\n",
    "参 数 说 明 ：\n",
    "arr： 输 入 数 组\n",
    "values： 要向 arr 添加的值， 需要和 arr 形状相同（除了要添加的轴）\n",
    "axis： 默认为 None。 当 axis 无定义时， 是横向加成， 返回总是为一维数组！ 当 axis 有定义的\n",
    "时候， 分别为 0 和 1 的时候。 当\n",
    "axis 有定义的时候， 分别为 0 和 1 的时候（列数要相同） 。 当 axis 为 1 时， 数组是加在右边\n",
    "（行数要相同） 。\n",
    "'''\n",
    "import numpy as np\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print(' 向 数 组 添 加 元 素 ： ')\n",
    "print(np.append(a, [7, 8, 9]))\n",
    "print('\\n')\n",
    "print('沿轴 0 添加元素： ')\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 添加元素： ')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n",
    "# 2. numpy.insert\n",
    "# 函数在给定索引之前， 沿给定轴在输入数组中插入值。 # 如果值的类型转换为要插入， 则它与输入数组不同。\n",
    "# 插入没有原地的， 函数会返回一个新数组。 此外， 如果未提供轴， 则输入数组会被展开。\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。 ')\n",
    "print(np.insert(a, 3, [11, 12]))\n",
    "print('\\n')\n",
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。 ')\n",
    "print('沿轴 0 广播： ')\n",
    "print(np.insert(a, 1, [11], axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 广播： ')\n",
    "print(np.insert(a, 1, 11, axis=1))"
   ],
   "id": "5d043245b7bfcfe3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组： \n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ： \n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "沿轴 0 添加元素： \n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n",
      "沿轴 1 添加元素： \n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n",
      "第一个数组： \n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。 \n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n",
      "传递了 Axis 参数。 会广播值数组来配输入数组。 \n",
      "沿轴 0 广播： \n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴 1 广播： \n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "13-数组的删除",
   "id": "f758092dae7c6937"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "source": [
    "#numpy.delete 函数返回从输入数组中删除指定子数组的新数组。 与 insert() 函数的情况一样，\n",
    "# 如果未提供轴参数， 则输入数组将展开。\n",
    "'''\n",
    "参 数 说 明 ： arr： 输 入 数 组\n",
    "obj： 可以被切片， 整数或者整数数组， 表明要从输入数组删除的子数组 axis：\n",
    "沿着它删除给定子数组的轴， 如果未提供， 则输入数组会被展开'''\n",
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。 ')\n",
    "print(np.delete(a,5))\n",
    "print('\\n')\n",
    "print('删除每一行中的第二列： ')\n",
    "print(np.delete(a,1,axis = 1))\n",
    "print('\\n')"
   ],
   "id": "6f80b4e67b157287",
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "14-数组的去重",
   "id": "cdb027c359b7c086"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# numpy.unique 函数用于去除数组中的重复元素。\n",
    "'''\n",
    "arr： 输入数组， 如果不是一维数组则会展开\n",
    "return_index： 如果为 true， 返回新列表元素在旧列表中的位置（下标） ， 并以列表形式储\n",
    "return_inverse： 如果为 true， 返回旧列表元素在新列表中的位置（下标） ， 并以列表形式储\n",
    "return_counts： 如果为 true， 返回去重数组中的元素在原数组中的出现次数\n",
    "'''\n",
    "import numpy as np\n",
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "print ('第一个数组： ')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('第一个数组的去重值： ')\n",
    "u = np.unique(a)\n",
    "print (u)\n",
    "print ('\\n')\n",
    "print ('去重数组的索引数组： ')\n",
    "u,indices = np.unique(a, return_index = True)\n",
    "print (indices)\n",
    "print ('\\n')\n",
    "print ('我们可以看到每个和原数组下标对应的数值： ')\n",
    "print (a)\n",
    "print ('去重数组的下标： ')\n",
    "u,indices = np.unique(a,return_inverse = True)\n",
    "print (u)\n",
    "print (indices)\n",
    "print ('\\n')\n",
    "print ('返回去重元素的重复数量： ')\n",
    "u,indices = np.unique(a,return_counts = True)\n",
    "print (u)\n",
    "print (indices)"
   ],
   "id": "718be1e49d360bc4"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "15-numpy的计算",
   "id": "dff1a1afd0f5e566"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import numpy as np\n",
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score,axis=0)\n",
    "print(result)\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(result)\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result)\n",
    "# 5. 数据的比较\n",
    "result = np.maximum( [-2, -1, 0, 1, 2], 0 ) # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(result)\n",
    "result = np.minimum( [-2, -1, 0, 1, 2], 0 ) # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(result)\n",
    "result = np.maximum( [-2, -1, 0, 1, 2], [1, 2, 3, 4, 5] )\n",
    "print(result)\n",
    "# 接受的两个参数， 也可以大小一致;第二个参数只是一个单独的值时， 其实是用到了维度的广播机制；\n",
    "# 6. 求平均值\n",
    "result = np.mean(score) # 获取所有数据的平均值\n",
    "print(result)\n",
    "result = np.mean(score,axis=0) # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "# 7. 求前缀和\n",
    "arr = np.array([[1,2,3], [4,5,6]])\n",
    "print(arr)\n",
    "print(arr.cumsum(0))\n",
    "'''\n",
    "[1, 2, 3]------> |1 |2 |3 |\n",
    "[4, 5, 6]------> |5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(1))\n",
    "'''\n",
    "[1, 2, 3]------> |1 |2+1 |3+2+1 |\n",
    "[4, 5, 6]------> |4 |4+5 |4+5+6 |\n",
    "'''\n",
    "# 8. argmin 求最小值索引\n",
    "result = np.argmin(score,axis=0)\n",
    "res=np.min(score,axis=0) #这样我们就可以知道最小的 81 是第二排的， 是从前往后遍历的\n",
    "print(result,res)\n",
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 标准差是一组数据平均值分散程度的一种度量。 一个较大的标准差， 代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差， 代表这些数据较接近平均值反应出数据的波动稳定情况， 越大表示波动越大， 越不稳定。\n",
    "result = np.std(score,axis=0)\n",
    "print(result)\n",
    "# 10. 极 值\n",
    "result = np.ptp(score,axis=None)#就是最大值和最小值的差\n",
    "print(result)"
   ],
   "id": "d701775251886d67"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "16-数组的拼接",
   "id": "b9ddd7c154830dc4"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 有的时候我们需要将两个数据加起来一起研究分析， 我们就可以将其进行拼接然后分析\n",
    "import numpy as np\n",
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "# 要求 a,b 两个数组的维度相同\n",
    "# print ('沿轴 0 连接两个数组： ')\n",
    "print (np.concatenate((a,b),axis= 0))\n",
    "print ('\\n')\n",
    "print ('沿轴 1 连接两个数组： ')\n",
    "print (np.concatenate((a,b),axis = 1))\n",
    "# 2. 根据轴进行堆叠\n",
    "print ('沿轴 0 连接两个数组： ')\n",
    "print (np.stack((a,b),axis= 0))\n",
    "print ('\\n')\n",
    "print ('沿轴 1 连接两个数组： ')\n",
    "print (np.stack((a,b),axis = 1))\n",
    "# 3. 矩阵垂直拼接\n",
    "v1 = [[0,1,2,3,4,5],\n",
    "[6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "[18,19,20,21,22,23]]\n",
    "result = np.vstack((v1,v2))\n",
    "print(result)\n",
    "# 4. 矩阵水平拼接\n",
    "v1 = [[0,1,2,3,4,5],\n",
    "[6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "[18,19,20,21,22,23]]\n",
    "result = np.hstack((v1,v2))\n",
    "print(result)"
   ],
   "id": "d4cd7d7429314f3b"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "17-数组的拆分",
   "id": "90796d358da0dcd1"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 1. 将一个数组分割为多个子数组\n",
    "'''\n",
    "参数说明：\n",
    "ary： 被分割的数组\n",
    "indices_or_sections： 是一个整数， 就用该数平均切分， 如果是一个数组， 为沿轴切分的位置\n",
    "（左开右闭）\n",
    "axis： 沿着哪个维度进行切向， 默认为 0， 横向切分。 为 1 时， 纵向切分\n",
    "'''\n",
    "import numpy as np\n",
    "arr = np.arange(9).reshape(3,3)\n",
    "print ('将数组分为三个大小相等的子数组： ')\n",
    "b = np.split(arr,3)\n",
    "print (b)\n",
    "# 2.numpy.hsplit 函数用于水平分割数组， 通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "harr = np.floor(10*np.random.random((2, 6)))\n",
    "print ('原 array： ')\n",
    "print(harr)\n",
    "print ('拆分后： ')\n",
    "print(np.hsplit(harr, 3))\n",
    "# 3.numpy.vsplit 沿着垂直轴分割\n",
    "a = np.arange(16).reshape(4,4)\n",
    "print ('第一个数组： ')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('竖直分割： ')\n",
    "b = np.vsplit(a,2)\n",
    "print (b)"
   ],
   "id": "1cfc2513c000a5ed"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "18-数组中nan和inf",
   "id": "9d873065d0ee48f4"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "C 语言中表示最大的正整数值是 0x7FFF FFFF， 最小的负整数是 0x8000 0000。\n",
    "查阅资料后， 发现 inf 表示无穷大， 需要使用 float(‘inf’ ) 函数来转化， 那么对应的就有\n",
    "float('-inf') 表示无穷小了。\n",
    "这样你就可以使用任意数来判断和它的关系了。\n",
    "那什么时候会出现 inf 呢？ 比如一个数字除以 0， Python 中会报错， 但是 numpy 中会是一个\n",
    "inf\n",
    "或者-inf\n",
    "另外还有 nan， 这种写法在 pandans 中常见， 表示缺失的数据， 所以一般用 nan 来表示。\n",
    "任何与其做运算结果都是 nan（比如零除以零， 在 numpy 中）\n",
    "'''\n",
    "import numpy as np\n",
    "# 创建一个 nan 和 inf #\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a))\n",
    "print(b,type(b))\n",
    "# --判断数组中为 nan 的个数\n",
    "t = np.arange(24,dtype=float).reshape(4,6)\n",
    "# 将三行四列的数改成 nan\n",
    "t[3,4] = np.nan\n",
    "# 可以使用 np.count_nonzero() 来判断非零的个数\n",
    "print(np.count_nonzero(t))\n",
    "# 并 且 np.nan != np.nan 结果 是 TRUE\n",
    "# 所以我们可以使用这两个结合使用判断 nan 的个数\n",
    "print(np.count_nonzero(t != t))\n",
    "# 将 nan 替换为 0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)\n",
    "# ----------练习： 处理数组中 nan\n",
    "t = np.arange(24).reshape(4,6).astype('float')\n",
    "# #将数组中的一部分替换 nan\n",
    "t[1,3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "# 遍历每一列， 然后判断每一列是否有 nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:,i]\n",
    "    # 判断当前列的数据中是否含有 nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有 nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为 nan 的数据拿出来\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        # 将 nan 替换成这一列的平均值\n",
    "        temp_col[np.isnan( temp_col )] = np.mean( temp_col_not_nan )\n",
    "print(t)"
   ],
   "id": "2bf35d30916a7836"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "18-转置",
   "id": "9c682a71be50379f"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#对换数组的维度\n",
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组： ')\n",
    "print (a )\n",
    "print ('\\n')\n",
    "print ('对换数组： ')\n",
    "print (np.transpose(a))\n",
    "# 与 transpose 一致\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组： ')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('转置数组： ')\n",
    "print (a.T)\n",
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re = t1.swapaxes(1,0)\n",
    "print (' 原 数 组 ： ')\n",
    "print (t1)\n",
    "print ('\\n')\n",
    "print ('调用 swapaxes 函数后的数组： ')\n",
    "print (re)\n",
    "t=np.rollaxis(t,3,0)\n",
    "print(t.shape)"
   ],
   "id": "d1121464944f0a8e"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
